package com.gujiayue.module.sys.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.cursor.Cursor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gujiayue.common.page.Page;
import com.gujiayue.common.restful.RestfulRtnInfo;
import com.gujiayue.common.utils.ConstantUtils;
import com.gujiayue.common.utils.JsonUtils;
import com.gujiayue.common.utils.PageUtils;
import com.gujiayue.module.sys.dao.SysUsrDao;
import com.gujiayue.module.sys.pojo.SysUsrDo;
import com.gujiayue.module.sys.pojo.copier.SysUsrCopier;
import com.gujiayue.module.sys.pojo.dto.SysUsrDto;
import com.gujiayue.module.sys.pojo.query.QuerySysUsrList;
import com.gujiayue.module.sys.pojo.query.QuerySysUsrPage;
import com.gujiayue.module.sys.service.SysUsrService;

@Service
@Transactional(readOnly = true)
public class SysUsrServiceImpl implements SysUsrService {

	@Resource
	private SysUsrDao sysUsrDao;

	@Override
	public RestfulRtnInfo getSysUsr(String userId) {
		return new RestfulRtnInfo(200, "查询成功", sysUsrDao.findList(SysUsrDo.builder().userId(userId).build()).get(0));
	}

	@Override
	public RestfulRtnInfo findList(QuerySysUsrList query) {
		SysUsrDo sysUsr=SysUsrCopier.instance.copierQuerySysUsrList(query);
		return new RestfulRtnInfo(200, "查询成功", sysUsrDao.findList(sysUsr));
	}
	
	@Override
	public RestfulRtnInfo pageList(QuerySysUsrPage query) {
		//校验分页参数
		RestfulRtnInfo verifyPage = PageUtils.verifyPage(query);
		if (!ConstantUtils.STATUS_SUCCESS.equals(verifyPage.getStatusCode())) {
			return verifyPage;
		}
		SysUsrDo sysUsr=SysUsrCopier.instance.copierQuerySysUsrPage(query);
		int pageCount=sysUsrDao.pageCount(sysUsr);
		Page<SysUsrDo> page=new Page<SysUsrDo>(query,pageCount);
//		sysUsr.setPageSql(PageUtils.pageSql(page));
		List<SysUsrDo> list=sysUsrDao.findList(sysUsr);
		page.setList(list);
		return PageUtils.viewPage(page);
	}

	@Override
	public RestfulRtnInfo save(SysUsrDto sysUsrDto) {
		SysUsrDo sysUsrDo=SysUsrCopier.instance.copierSysUsrDto(sysUsrDto);
		System.out.println(JsonUtils.obeyJson(sysUsrDo));
		return new RestfulRtnInfo(200, "保存成功");
	}

	@Override
	public RestfulRtnInfo delete(String userId) {
		sysUsrDao.delete(SysUsrDo.builder().userId(userId).build());
		return new RestfulRtnInfo(200, "删除成功");
	}

	@Override
	public RestfulRtnInfo cursorPage(String limit) {
		
		Cursor<SysUsrDo> cursor=sysUsrDao.cursorPage(limit);
		
		cursor.forEach(usrDo -> { 
			System.out.println("--->"+usrDo.getLoginName());
		});
		
		return new RestfulRtnInfo(200, "成功",cursor);
	}
}
